package com.aistar.service.impl;

import com.aistar.pojo.Comment;
import com.aistar.pojo.CommentVo;
import com.aistar.pojo.Customer;
import com.aistar.repository.CommentRepository;
import com.aistar.service.CommentImagesService;
import com.aistar.service.CommentService;
import com.aistar.service.CustomerService;
import com.aistar.service.ProductService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private CommentImagesService commentImagesService;

    @Autowired
    private ProductService productService;

    @Autowired
    private CustomerService customerService;

    @Override
    public ServerResponse save(Comment comment) {
       Comment save =  commentRepository.save(comment);
       if (save != null){
           System.out.println("保存成功");
           return ServerResponse.getDataSuccess(save);
       }else {
           return ServerResponse.addFailed();
       }

    }

    @Override
    public ServerResponse modifyStatus(Comment comment) {
        return null;
    }

    @Override
    public ServerResponse modify(Comment comment) {
        return null;
    }

    @Override
    public ServerResponse getById(Integer id) {
        return null;
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }


    //根据商品id获取到该商品的评论数
    @Override
    public ServerResponse commentCount(Integer productId) {
        List<Comment> commentList = commentRepository.findByProductId(productId);
        return ServerResponse.getDataSuccess(commentList.size());

    }
    //根据商品id获取到所有的评论  分页
    @Override
    public ServerResponse getAllCommentByProductId(Integer productId, Integer page) {
        //用于接收 评论 用户 评论图片集合
        List<CommentVo> commentVoList = new ArrayList<>();
        Pageable pageable = PageRequest.of(page-1,3, Sort.by(Sort.Direction.DESC,"commentTime"));
        Integer status = 1;
        Page<Comment> all = commentRepository.findAllByProductIdAndCommentStatus(productId,status,pageable);
        //Page<Comment> all = commentRepository.findByProductId(productId,pageable);
        List<Comment> commentList = all.getContent();
        if (commentList != null && commentList.size()>0){
           //从集合中循环去除commentId 来查询对应的图片的集合
            for (Comment comment : commentList){
                //获取评论用户的信息
                Customer customer = (Customer)customerService.getById(comment.getCustomerId()).getData();
                //获取评论对应的图片集合
               /* ServerResponse serverResponse = commentImagesService.getAllByCommentId(comment.getCommentId());
                //将图片集合  判断   并取出来
                List<CommentImages> commentImagesList = null;

                if (serverResponse.getStatus()==1)
                    commentImagesList = (List<CommentImages>) serverResponse.getData();*/

                //将数据封装进vo对象中
                commentVoList.add(new CommentVo(comment,customer,null));
            }
            return ServerResponse.getDataSuccess(commentVoList);

        }else {
            return ServerResponse.getDataFailed("查询失败，并无数据 ");
        }


    }

    //根据用户id查询到用户的评论
    @Override
    public ServerResponse getAllCommentByCustomerId(Integer customerId) {
        List<Comment> commentList = commentRepository.findByCustomerId(customerId);
        return ServerResponse.getDataSuccess(commentList);

    }
}
